package com.start.quartz.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.start.common.api.Result;
import com.start.common.dataRule.DataQueryConstructor;
import com.start.core.annotations.RegisterMenu;
import com.start.core.annotations.RegisterPermission;
import com.start.core.annotations.StartLog;
import com.start.quartz.entity.StartQuartzEntity;
import com.start.quartz.model.StartQuartzEntityModel;
import com.start.quartz.service.StartQuartzService;
import com.start.security.annotations.DataPermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


/**
 * 定时任务
 *
 * @author gxl
 * @email ${email}
 * @date 2021-01-04 11:00:51
 */
@Tag(name = StartQuartzController.LOG_TITLE)
@RestController
@RequestMapping("start/quartz")
@RegisterMenu(value = "定时任务", systemCode = "start:quartz", component = "views/start/quartz/index", path = "/quartz/index")
public class StartQuartzController {
    public static final String LOG_TITLE = "定时任务" ;

    @Autowired
    private StartQuartzService startQuartzService;


    /**
     * 列表
     */
    @Operation(summary = "分页查询定时任务")
    @GetMapping("/page")
    @DataPermission("views/start/quartz/index")
    public Result<?> list(Page<StartQuartzEntityModel> page, StartQuartzEntityModel entity) {
        QueryWrapper<StartQuartzEntityModel> queryWrapper = DataQueryConstructor.createEntity(entity);
        Page<StartQuartzEntityModel> entityPage = startQuartzService.pageQuartzEntityModel(page, queryWrapper);
        return Result.ok(entityPage);
    }


    /**
     * 信息
     */
    @Operation(summary = "查询定时任务详情")
    @GetMapping("/info/{id}")
    @StartLog(value = LOG_TITLE, action = "查询")
    @PreAuthorize("@pas.hasPermission('start:quartz:info')")
    @RegisterPermission(value = "start:quartz:info", name = "查看")
    public Result<?> info(@PathVariable("id") String id) {
        return Result.ok(startQuartzService.getById(id));
    }

    /**
     * 保存
     */
    @Operation(summary = "添加定时任务")
    @PostMapping("/add")
    @StartLog(value = LOG_TITLE, action = "添加")
    @PreAuthorize("@pas.hasPermission('start:quartz:add')")
    @RegisterPermission(value = "start:quartz:add", name = "添加")
    public Result<?> save(@RequestBody StartQuartzEntity sysQuartz) {
        startQuartzService.save(sysQuartz);
        return Result.ok();
    }

    /**
     * 修改
     */
    @Operation(summary = "修改定时任务")
    @PutMapping("/updateById")
    @StartLog(value = LOG_TITLE, action = "修改")
    @PreAuthorize("@pas.hasPermission('start:quartz:edit')")
    @RegisterPermission(value = "start:quartz:edit", name = "修改")
    public Result<?> update(@RequestBody StartQuartzEntity sysQuartz) throws SchedulerException {
        startQuartzService.updateTask(sysQuartz);
        return Result.ok();
    }

    @Operation(summary = "激活定时任务")
    @StartLog(value = LOG_TITLE, action = "激活任务")
    @PreAuthorize("@pas.hasPermission('start:quartz:active')")
    @RegisterPermission(value = "start:quartz:active", name = "激活任务")
    @GetMapping("/activeById/{id}")
    public Result<?> activeById(@PathVariable("id") String id) {
        startQuartzService.activeTask(id);
        return Result.ok();
    }

    @Operation(summary = "禁用定时任务")
    @StartLog(value = LOG_TITLE, action = "禁用任务")
    @PreAuthorize("@pas.hasPermission('start:quartz:pause')")
    @RegisterPermission(value = "start:quartz:pause", name = "禁用任务")
    @GetMapping("/disableById/{id}")
    public Result<?> disableById(@PathVariable("id") String id) throws SchedulerException {
        startQuartzService.disableTask(id);
        return Result.ok();
    }

    /**
     * 删除
     */
    @Operation(summary = "删除定时任务")
    @DeleteMapping("/deleteById/{id}")
    @StartLog(value = LOG_TITLE, action = "删除")
    @PreAuthorize("@pas.hasPermission('start:quartz:delete')")
    @RegisterPermission(value = "start:quartz:delete", name = "删除")
    public Result<?> delete(@PathVariable("id") String id) {
        startQuartzService.deleteTask(id);
        return Result.ok();
    }

    @Operation(summary = "暂停定时任务")
    @StartLog(value = LOG_TITLE, action = "暂停任务")
    @PreAuthorize("@pas.hasPermission('start:quartz:pause')")
    @RegisterPermission(value = "start:quartz:pause", name = "暂停任务")
    @GetMapping("/pauseById/{id}")
    public Result<?> pauseById(@PathVariable("id") String id) throws SchedulerException {
        startQuartzService.pauseTask(id);
        return Result.ok();
    }

    @Operation(summary = "暂停全部定时任务")
    @StartLog(value = LOG_TITLE, action = "暂停全部任务")
    @PreAuthorize("@pas.hasPermission('start:quartz:pauseAll')")
    @RegisterPermission(value = "start:quartz:pauseAll", name = "暂停全部任务")
    @GetMapping("/pauseAll")
    public Result<?> pauseAll() throws SchedulerException {
        startQuartzService.pauseAll();
        return Result.ok();
    }

    @Operation(summary = "恢复定时任务")
    @StartLog(value = LOG_TITLE, action = "恢复任务")
    @PreAuthorize("@pas.hasPermission('start:quartz:recovery')")
    @RegisterPermission(value = "start:quartz:recovery", name = "恢复任务")
    @GetMapping("/recoveryById/{id}")
    public Result<?> recoveryById(@PathVariable("id") String id) throws SchedulerException {
        startQuartzService.recovery(id);
        return Result.ok();
    }

    @Operation(summary = "恢复全部定时任务")
    @StartLog(value = LOG_TITLE, action = "恢复全部任务")
    @PreAuthorize("@pas.hasPermission('start:quartz:recoveryAll')")
    @RegisterPermission(value = "sys:sysQuartz:recoveryAll", name = "恢复全部任务")
    @GetMapping("/recoveryAll")
    public Result<?> recoveryAll() throws SchedulerException {
        startQuartzService.recoveryAll();
        return Result.ok();
    }

    @Operation(summary = "立即执行定时任务")
    @StartLog(value = LOG_TITLE, action = "立即执行任务")
    @PreAuthorize("@pas.hasPermission('sys:sysQuartz:start')")
    @RegisterPermission(value = "sys:sysQuartz:start", name = "立即执行任务")
    @GetMapping("/startById/{id}")
    public Result<?> start(@PathVariable("id") String id) throws SchedulerException {
        startQuartzService.startTask(id);
        return Result.ok();
    }


}
